package com.daoliuhe.scaffold.web.controller.admin;

import com.daoliuhe.scaffold.model.User;
import com.daoliuhe.scaffold.model.UserGroupUser;
import com.daoliuhe.scaffold.model.UserRoleRelation;
import com.daoliuhe.scaffold.service.*;
import com.daoliuhe.scaffold.web.dto.UserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource
    UserService userService;

    @Autowired
    UserRoleRelationService userRoleRelationService;

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/list")
    public ModelAndView list(User user) throws UnsupportedEncodingException {
        logger.info("list,user:{}", user);
        ModelAndView mav = new ModelAndView("admin/user/list");
        mav.addAllObjects(userService.getPageData(user));
        mav.addObject("entity", user);
        return mav;
    }


    @RequestMapping("/edit")
    public ModelAndView edit(User user, @RequestParam(required = false) String tab, UserDto userDto) {
        logger.info("edit,user:{},tab:{},userDto{}", user, tab, userDto);
        ModelAndView mav = new ModelAndView("admin/user/edit");
        if (!StringUtils.isEmpty(user.getId())) {
            User u = userService.getUserById(user.getId());
            u.setPage(user.getPage());
            u.setRows(user.getRows());
            mav.addObject("entity", u);
            mav.addObject("userDto", userDto);

            // 用户角色关系分页
            UserRoleRelation ur = new UserRoleRelation();
            ur.setUserId(user.getId());
            ur.setPage(userDto.getrPage());
            ur.setRows(userDto.getrRows());
            mav.addObject("roleMap", userRoleRelationService.getRolePageData(ur));
            userDto.setrPage(ur.getPage());

            // 用户关联的用户组分页
            UserGroupUser ugu = new UserGroupUser();
            ugu.setUserId(new BigDecimal(user.getId()));
            ugu.setPage(userDto.getuPage());
            ugu.setRows(userDto.getuRows());
            mav.addObject("userGroupMap", userGroupService.getUserGroupPageData(ugu));
            userDto.setuPage(ugu.getPage());

            mav.addObject("tab", tab);
        }
        mav.addObject("param", user);
        return mav;
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(User user) {
        logger.info("saveOrUpdate,user:{}", user);
        userService.saveOrUpdate(user);
        return "redirect:/user/list";
    }


    @RequestMapping("/updateBatchEnable")
    public String updateBatchEnable(String userIds) {
        logger.info("saveOrUpdate,ids:{}", userIds);
        String[] idArray = userIds.split(";");
        ArrayList<String> ids = new ArrayList<String>();
        for (String id : idArray) {
            ids.add(id);
        }
        userService.updateBatchEnable(ids);
        return "redirect:/user/list";
    }

    @RequestMapping("/updateBatchDisable")
    public String updateBatchDisable(String userIds) {
        logger.info("saveOrUpdate,user:{}", userIds);
        String[] idArray = userIds.split(";");
        ArrayList<String> ids = new ArrayList<String>();
        for (String id : idArray) {
            ids.add(id);
        }
        userService.updateBatchDisable(ids);
        return "redirect:/user/list";
    }

    @RequestMapping("/data")
    @ResponseBody
    public Object data(User user) {
        logger.info("data,user:{}", user);
        return userService.getPageData(user);
    }

    @RequestMapping("/selectUser")
    @ResponseBody
    public Object selectUser(String param) {
        logger.info("selectUser,param:{}", param);
        return userService.selectUser(param);
    }

    @RequestMapping("/vLoginId")
    @ResponseBody
    public Object data(String loginId, String id) {
        logger.info("data,loginId:{},id:{}", loginId, id);
        return userService.isRightLoginId(loginId, id);
    }

    //根据登陆账号获取用户信息
    @RequestMapping("/getUserById")
    @ResponseBody
    public User getUserById(String id) {
        logger.info("getUserById,id:{}", id);
        return userService.getUserById(id);
    }
}
